/***********************************************
 *  
 *  A proxy representing the product service (model)
 *   NOTE: All methods return deferred object, see JQuery
 *  
 */
var ProductCatalogue = function(uri) {
    this.baseuri = uri;
};

ProductCatalogue.prototype = (function() {
    return {
        getAll: function() {
            return $.getJSON(this.baseuri);
        },
        add: function(name, price) {
            return $.ajax({
                dataType: "json",
                url: this.baseuri,
                type: 'POST',
                data: {name: name, price: price}
            });
        },
        remove: function(id) {
            return $.ajax({
                dataType: "text",
                url: this.baseuri+"/"+id,
                type: 'DELETE'
            });
        },
        update: function(id, name, price) {
            return $.ajax({
                dataType: "json",
                url: this.baseuri,
                type: 'PUT',
                data: {id: id, name: name, price: price}
            });
        },
        find: function(id) {
            return $.ajax({
                dataType: "json",
                url: this.baseuri+"/"+id,
                type: 'GET'
            });
        },
        getRange: function(first, nitems) {
            return $.ajax({
                dataType: "json",
                url: this.baseuri + "/query",
                type: 'GET',
                data: {first: first, nitems: nitems}
            });
        },
        getCount: function() {
          return $.ajax({
                dataType: "json",
                url: this.baseuri + "/count",
                type: 'GET'
            });  
        }
    };
}());
